/**
 * @description 行业类目管理控制器
 * @author 韩
 */

const { randomUUID } = require("crypto")
const dayjs = require("dayjs")
const class_model = require("../../../model/product/class/CLASS.MODEL")
const { getFilter } = require("../../../utils/setFilter")
// 创建一个内容
const class_controller_save = async ({
    id = undefined,
    parentId = undefined,
    className = undefined,
    sort = undefined,
    status = undefined
}) => {
    const params = {
        createDate: dayjs(),
        id: randomUUID(), parentId, className, sort, status
    }
    let addRes = null;
    if (!id) {
        // 添加创建
        addRes = await class_model.create({
            ...params
        })
    } else {
        // 更新修改
        addRes = await class_model.findOneAndUpdate({ id: id }, {
            $set: {
                ...params
            }
        }, { new: true })
    }

    if (addRes) {
        const {
            id, parentId, className, sort, status
        } = addRes

        return {
            code: 0,
            msg: "成功",
            data: {
                id, parentId, className, sort, status
            }
        }
    }
    return {
        code: "2000",
        msg: "失败",
    }
}



// 查询列表 
const class_controller_find = async ({ pageSize, current, className = undefined, status = undefined }) => {
    try {
        console.log("查询条件：", className, status)
        let filter = {
            className: className && { $regex: eval("/" + className + "/i") },//名称模糊查询
            status: status && { $in: status }
        }
        filter = getFilter(filter)
        const list = await class_model.find(filter).sort({ sort: 1 }).skip((current - 1) * pageSize).limit(pageSize).lean()
        const totalCount = await class_model.countDocuments(filter);
        return {
            code: 0,
            msg: "查询成功",
            data: {
                list,
                totalCount
            }
        }
    } catch (error) {
        return {
            code: 12,
            msg: "查询失败",
        }
    }
}


/**@description 根据id查详情 */
const class_controller_get = async ({ id }) => {
    const res = await class_model.findOne({
        id
    })
    if (res) {
        const { id, parentId, className, sort, status } = res;

        return {
            code: 0,
            message: "查询成功",
            data: {
                id, parentId, className, sort, status
            }
        }
    } else {
        return {
            code: '01',
            msg: "未找到对应菜单数据"
        }
    }
}

// 删除
const class_controller_delete = async ({ ids = [] }) => {
    const deleteResult = await class_model.deleteMany({
        id: { $in: ids }
    });

    if (deleteResult.deletedCount) {
        return {
            code: 0,
            msg: `删除了${deleteResult.deletedCount}条数据`
        }
    } else {
        return {
            code: '0010',
            msg: `删除失败`
        }
    }

}
module.exports = {
    class_controller_save,
    class_controller_find,
    class_controller_get,
    class_controller_delete,
}